// ignore_for_file: dead_code

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:leader_doctor_flutter/common/data/app.dart';
import 'package:leader_doctor_flutter/common/widget/comm_widget.dart';
import 'package:leader_doctor_flutter/common/utils/string_utils.dart';
import 'package:leader_doctor_flutter/common/utils/image_utils.dart';
import 'package:leader_doctor_flutter/plugins/plugins_messages.g.dart';
import 'package:leader_doctor_flutter/styles/colors.dart';
import 'package:url_launcher/url_launcher.dart';

class CancelAccountPage extends StatefulWidget {
  const CancelAccountPage({super.key});

  @override
  State<CancelAccountPage> createState() => _CancelAccountPageState();
}

class _CancelAccountPageState extends State<CancelAccountPage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var userInfo = App.instance.userInfo;
    var serviceInfo = App.instance.serviceInfo;
    String phone = StringUtils.encryptPhoneNumber(userInfo?.phone ?? '');
    String servicePhone = serviceInfo?.phone ?? '';
    String email = serviceInfo?.email ?? '';
    String time = serviceInfo?.workingTime ?? '';

    return Scaffold(
      appBar: CommWidget.getCommAppbar(context),
      body: Container(
        // padding: EdgeInsets.only(left: 20.sp, right: 20.sp),
        width: double.infinity,
        height: double.infinity,
        color: ColorRes.colorFFFFFF,
        child: Column(
          children: [
            Expanded(
              child: SingleChildScrollView(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Container(
                      alignment: Alignment.center,
                      margin: EdgeInsets.only(top: 32.w),
                      child: ImageLoader.loadAsset('icon_cancel_account_tip.webp', 58.w),
                    ),
                    SizedBox(height: 20.h),
                    Text(
                      "将${phone}账号注销",
                      style: TextStyle(fontSize: 18.sp, color: Color(0xFF333333), fontWeight: FontWeight.bold),
                    ),
                    SizedBox(
                      height: 5.h,
                    ),
                    Text(
                      "注意：注销后无法恢复，请谨慎操作",
                      style: TextStyle(
                        fontSize: 16.sp,
                        color: Color(0xFF666666),
                      ),
                    ),
                    SizedBox(
                      height: 5.h,
                    ),
                    Container(
                      margin: EdgeInsets.only(left: 15, right: 15),
                      alignment: Alignment.center,
                      child: Text(
                        "收到您的注销请求后我们一般在48小时内处理，最长不超过15个工作日",
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          fontSize: 16.sp,
                          color: Color(0xFF666666),
                        ),
                      ),
                    ),
                    SizedBox(
                      height: 30.h,
                    ),
                    // 使用 IntrinsicHeight 包裹，确保 Container 高度根据内容高度调整
                    IntrinsicHeight(
                      child: Container(
                        alignment: Alignment.centerLeft,
                        margin: EdgeInsets.only(left: 15, right: 15),
                        padding: EdgeInsets.only(top: 20),
                        decoration: BoxDecoration(
                          color: ColorRes.colorF9F9FA,
                          borderRadius: BorderRadius.circular(6),
                        ),
                        child: Column(
                          children: [
                            buildTitleDescriptionWidget(title: "无法使用账户", description: "如果您确认注销，您的账号将无法登录领未来APP。"),
                            buildTitleDescriptionWidget(
                                title: "以下信息将被清空", description: "账户信息、第三方授权、认证身份、消息记录、权益信息、账户余额、订单记录、收藏、关注灯信息。"),
                          ],
                        ),
                        // child: buildTitleDescriptionWidget(
                        //     title: "无法使用账户", description: "如果您确认注销，您的账号将无法登录领未来APP。"),
                      ),
                    ),
                    SizedBox(
                      height: 30,
                    ),
                    customServiceInfo(email, servicePhone, time),
                  ],
                ),
              ),
            ),
            Container(
              height: 100,
              // width: double.infinity,
              padding: EdgeInsets.only(left: 15.w, right: 15.w),
              color: ColorRes.colorF9F9FA,
              alignment: Alignment.center,
              child: CommWidget.customBuildBtn("联系客服", () async {
                await NativeInterface().contactCustomerService();
              }, ColorRes.colorMain, ColorRes.colorMain, 4.w, isSelect: true),
            ),
          ],
        ),
      ),
    );
  }
}

// 构建整体的 UI 部分
Widget buildTitleDescriptionWidget({
  required String title,
  required String description,
}) {
  return Center(
    child: Container(
      margin: EdgeInsets.only(left: 20, right: 20, bottom: 20),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 蓝色圆点
          Container(
            padding: EdgeInsets.only(top: 6),
            child: buildBlueDot(),
          ),
          // 间隔
          SizedBox(width: 10),
          // 标题和描述
          Expanded(
            child: buildTextContent(title, description),
          ),
        ],
      ),
    ),
  );
}

// 构建蓝色圆点
Widget buildBlueDot() {
  return Container(
    margin: EdgeInsets.only(left: 5),
    width: 10,
    height: 10,
    decoration: BoxDecoration(
      color: ColorRes.colorMain,
      shape: BoxShape.circle,
    ),
  );
}

// 构建标题和描述
Widget buildTextContent(String title, String description) {
  return Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
      // 标题
      Text(
        title,
        style: TextStyle(
          fontWeight: FontWeight.bold,
          color: Color(0xFF333333),
          fontSize: 16,
        ),
      ),
      // 上下间隔
      SizedBox(height: 5),
      // 描述，自动换行
      Text(
        description,
        softWrap: true,
        style: TextStyle(
          color: Color(0xFF666666),
          fontSize: 15,
        ),
      ),
    ],
  );
}

// 客服信息
Widget customServiceInfo(String email, String phone, String time) {
  return Container(
    color: ColorRes.colorFFFFFF,
    margin: EdgeInsets.only(left: 15, right: 15),
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          "你可以联系客服进行账号注销：",
          style: TextStyle(fontSize: 15.sp, color: Color(0xFF333333), fontWeight: FontWeight.bold),
        ),
        SizedBox(
          height: 10,
        ),
        Text(
          "工作日：${time}",
          style: TextStyle(fontSize: 15.sp, color: ColorRes.color666666),
        ),
        SizedBox(
          height: 5,
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            // Text(
            //   "电话：",
            //   style: TextStyle(fontSize: 14, color: Color(0xFF333333), fontWeight: FontWeight.bold),
            // ),
            // dialTelephone(phone),
            // SizedBox(
            //   width: 0,
            // ),
            // Text(
            //   "（工作日：${time}）",
            //   style: TextStyle(fontSize: 14, color: Color(0xFF333333), fontWeight: FontWeight.bold),
            // ),
          ],
        ),
      ],
    ),
  );
}

// 客服电话样式及拨打电话
Widget dialTelephone(String phone) {
  return GestureDetector(
    onTap: () {
      launch("tel://${phone}");
    },
    child: Text(
      '${phone}',
      style: TextStyle(
        color: ColorRes.colorMain,
        fontSize: 14.sp,
        fontWeight: FontWeight.bold,
        decoration: TextDecoration.none,
      ),
    ),
  );
}
